package com.huajin.iot.system.controller;

import com.huajin.iot.common.config.Constant;
import com.huajin.iot.common.controller.BaseController;
import com.huajin.iot.common.domain.Tree;
import com.huajin.iot.common.utils.R;
import com.huajin.iot.system.domain.DeptDO;
import com.huajin.iot.system.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-27 14:40:36
 */
@Api(tags = {"部门管理接口"})
@RestController
@RequestMapping("/system/sysDept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService sysDeptService;


    @ApiOperation(value = "获取部门列表")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:sysDept:list")
    public R list() {
        Map<String, Object> query = new HashMap<>(16);
        List<DeptDO> sysDeptList = sysDeptService.list(query);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", sysDeptList);
        return R.ok(resultMap);
    }

    @ApiOperation("根据id查找部门")
    @GetMapping("/{pId}")
    @RequiresPermissions("system:sysDept:search")
    R getById(@PathVariable("pId") Long pId) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("pId", pId);
        if (pId == 0) {
            resultMap.put("pName", "总部门");
        } else {
            resultMap.put("pName", sysDeptService.get(pId).getName());
        }
        return R.ok(resultMap);
    }


    /**
     * 保存
     */
    @ApiOperation("新增部门")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:sysDept:add")
    public R save(DeptDO sysDept) {
        if (sysDeptService.save(sysDept) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ApiOperation("更新部门")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("system:sysDept:edit")
    public R update(DeptDO sysDept) {
        if (sysDeptService.update(sysDept) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @ApiOperation("删除单个部门")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:sysDept:remove")
    public R remove(Long deptId) {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", deptId);
        if (sysDeptService.count(map) > 0) {
            return R.error(1, "包含下级部门,不允许修改");
        }
        if (sysDeptService.checkDeptHasUser(deptId)) {
            if (sysDeptService.remove(deptId) > 0) {
                return R.ok();
            }
        } else {
            return R.error(1, "部门包含用户,不允许修改");
        }
        return R.error();
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除部门")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:sysDept:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] deptIds) {
        if (sysDeptService.batchRemove(deptIds) == deptIds.length) {
            return R.ok();
        } else {
            return R.error(1, "删除失败");
        }
    }

    @ApiOperation("部门树形表示")
    @GetMapping("/tree")
    @ResponseBody
    public Tree<DeptDO> tree() {
        Tree<DeptDO> tree;
        tree = sysDeptService.getTree();
        return tree;
    }

}
